package com.sduept.nwld.dataserver.manager.etl;

import com.sduept.core.dao.SuperDAO;
import com.sduept.generator.ObjectId;
import com.sduept.nwld.dataserver.manager.cim.CimManager;
import com.sduept.bigdata.fault.common.ETLDAO;
import com.sduept.nwld.dataserver.model.daily.OperationaRiskExceed;
import com.sduept.utils.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class OperationaRiskExceedETLManager {
	@Autowired
	private SuperDAO dao;
	@Autowired
	private ETLDAO eDao;
	@Autowired
	private CimManager cm;
	
	public  void receiveAndSaveAllData() {
		List<?> list = getAllSourceData();
		receiveAndSave(list);
	}
	public  void receiveAndSavePartData(String date) {
		List<?> list = getAllSourceData(date);
		receiveAndSave(list);
	}
	
	
	public List<?> getAllSourceData(){
		List <?>result = eDao.query("select zdxt,gdsw,gxsw,ynsw,gzsw,hnsw,searchDate from t_daily_protectrisk_exceed ");
		return result;
	}
	public List<?> getAllSourceData(String date) {
		String sql = "select zdxt,gdsw,gxsw,ynsw,gzsw,hnsw,searchDate from t_daily_protectrisk_exceed"
				+ " where update_time=";
		List<?> result = eDao.query(sql + "'" + date + "'");
		return result;
	}
	public  void receiveAndSave(List<?> list) {
		ETLDataSave equipmentSave = new ETLDataSave(list);
		equipmentSave.saveToBean();
	}
	public class ETLDataSave{
		private List<?> list;
 
		public ETLDataSave(List<?> list) {
			super();
			this.list = list;
		}
		public void saveToBean(){
			int updateCount=0;
			for(Object obj : list){
				OperationaRiskExceed objsaves=new OperationaRiskExceed();
				Object[] objs = (Object[]) obj;
				if(objs[0]!=null) objsaves.setDirectTransferNum(Integer.parseInt(objs[0].toString()));
				if(objs[1]!=null) objsaves.setGuangdongNum(Integer.parseInt(objs[1].toString()));
				if(objs[2]!=null) objsaves.setGuangxiNum(Integer.parseInt(objs[2].toString()));
				if(objs[3]!=null) objsaves.setYunnanNum(Integer.parseInt(objs[3].toString()));
				if(objs[4]!=null) objsaves.setGuizhouNum(Integer.parseInt(objs[4].toString()));
				if(objs[5]!=null) objsaves.setHainanNum(Integer.parseInt(objs[5].toString()));
				if(objs[6]!=null) {
					objsaves.setRecordDate(DateUtil.getDateByString(objs[6].toString(), DateUtil.pattern_ymd));
					List<OperationaRiskExceed> condition = dao.findByCondition(OperationaRiskExceed.class, "recordDate=?", objsaves.getRecordDate());
					if(condition!=null&&condition.size()>0) {
						OperationaRiskExceed operationaRiskExceed = condition.get(0);
						objsaves.setId(operationaRiskExceed.getId());
						dao.createOrUpdate(objsaves, OperationaRiskExceed.class);
						updateCount++;
					}else {
						objsaves.setId(ObjectId.get().toString());
						dao.createOrUpdate(objsaves, OperationaRiskExceed.class);
						updateCount++;
					}
				}
					
			}
			log.info("更新保护风险管理-超期未检信息："+updateCount);
		}
	}
}
